package com.jsmtr.rms.controller;

import java.util.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jsmtr.rms.entity.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jsmtr.rms.service.IRecommendService;
import com.jsmtr.rms.service.IDoZZService;
import com.jsmtr.rms.service.ISchoolService;
import com.jsmtr.rms.service.IUtilService;
import com.jsmtr.rms.utils.EnumTools;


@Controller
@RequestMapping("/Do/ZZReview")
public class DoZZReviewController {

    @Resource
    private IUtilService utilService;

    @Resource
    private IRecommendService recommendService;

    @Resource
    private ISchoolService schoolService;

    @Resource
    private IDoZZService doZZService;


    //[start] 查询毕业生信息

    //首页
    @RequestMapping("/")
    public String Index(HttpServletRequest request, Model model) {
        try {
            model.addAttribute("path", request.getContextPath());
            String pid = request.getParameter("pid").trim();
            String fid = request.getParameter("fid").trim();
            String deptcode = request.getParameter("deptcode").trim();

            model.addAttribute("pid", pid);
            model.addAttribute("fid", fid);
            model.addAttribute("deptcode", deptcode);

            StringBuilder str = new StringBuilder();
            List<HRSchoolUsers> schools = schoolService.getSchools();
            for (HRSchoolUsers school : schools) {
                str.append("<option value='" + school.getId() + "'>" + school.getUname() + "</option>");
            }
            model.addAttribute("school", str);
            //招聘岗位
            List<ProjectInfoJobs> projectJobs=utilService.getProjectJobs(pid);
            model.addAttribute("jobs",projectJobs);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Do/ZZCampus/Review/index";
    }

    @SuppressWarnings("unchecked")
    @RequestMapping("/getusers")
    public void getusers(HttpServletRequest request, HttpServletResponse response, HRZZUser user) {
        try {
            int perpage = Integer.parseInt(request.getParameter("perpage"));
            int page = Integer.parseInt(request.getParameter("page"));
            String flowid = request.getParameter("flowid").trim();
            user.setPath("/" + flowid + "/");
            Map<String, Object> _map = new HashMap<String, Object>();
            _map.put("agefrom", request.getParameter("agefrom")==null?"":request.getParameter("agefrom").trim());
            _map.put("ageto", request.getParameter("ageto")==null?"":request.getParameter("ageto").trim());
            _map.put("heightfrom", request.getParameter("heightfrom")==null?"":request.getParameter("heightfrom").trim());
            _map.put("heightto", request.getParameter("heightto")==null?"":request.getParameter("heightto").trim());
            _map.put("rcount",request.getParameter("graduationyear")==null?"":request.getParameter("graduationyear").trim());
            Map<String, Object> map = recommendService.getUserMap(page, perpage, user,_map);
            StringBuilder result = new StringBuilder();
            double pageCount = (Double) map.get("pageCount");
            double allcount = (Double) map.get("count");

            if (pageCount != 0) {
                List<HashMap> object = (List<HashMap>)map.get("list");
                int num = 0;
                //20181108 cyx更改为读取hashmap数据不再强转为实体
                /*for (HashMap<String,String> _user :  object) {
                    result.append("<tr><td><input type=\"checkbox\" name='uid' value='" + _user.getId() + "' id='cb_" + _user.getId() + "' /></td>");
                    result.append("<td>" + (perpage * (page - 1) + num + 1) + "</td>");
                    result.append("<td>" + _user.getName() + "</td>");
                    result.append("<td>" + _user.getGender() + "</td>");
                    result.append("<td>" + _user.getCode() + "</td>");
                    result.append("<td>" + _user.getMajor() + "</td>");
                    result.append("<td>" + _user.getGraduationtime() + "</td>");
                    result.append("<td>" + _user.getBx() + "</td>");
                    result.append("<td>" + _user.getGuake() + "</td>");
                    result.append("<td>" + _user.getTc() + "</td>");
                    result.append("<td>" + _user.getDeptname() + "<input type='hidden' name='user[" + num + "].id' value='" + _user.getId() + "' /></td>");
                    result.append("<td>");
                    if (_user.getState().equals(2)) {
                        result.append("已通过");
                    } else if (_user.getState().equals(3)) {
                        result.append("未通过");
                    } else {
                        result.append("审核中");
                    }

                    result.append("</td>");
                    if (_user.getOp1().equals("未公布")) {
                        result.append("<td class='true'><span>" + _user.getOp1() + "</span></td>");
                    } else {
                        result.append("<td class='false'><span>" + _user.getOp1() + "</span></td>");
                    }
                    if (_user.getFqstate().equals("已放弃")
                            && _user.getFqfid().equals(flowid)) {
                        result.append("<td class='yfq'><span>已放弃</span></td></tr>");
                    } else {
                        if (_user.getState() == EnumTools.zzuserState.未通过.ordinal()) {
                            result.append("<td class='false'><span>已完成</span></td></tr>");
                        } else {
                            boolean bl = _user.getFid().equals(flowid);
                            result.append("<td class='" + bl + "'><span>" + (bl == true ? "未完成" : "已完成") + "</span></td></tr>");
                        }
                    }*/
                for (HashMap<String,Object> _user :  object) {
                    result.append("<tr><td><input type=\"checkbox\" name='uid' value='" + _user.get("ID") + "' id='cb_" + _user.get("ID") + "' /></td>");
                    result.append("<td>" + (perpage * (page - 1) + num + 1) + "</td>");
                    result.append("<td>" + _user.get("NAME") + "</td>");
                    result.append("<td>" + _user.get("GENDER") + "</td>");
                    result.append("<td>" + _user.get("CODE") + "</td>");
                    result.append("<td>" + _user.get("MAJOR") + "</td>");
                    result.append("<td>" + _user.get("GRADUATIONTIME") + "</td>");
                    result.append("<td>" + _user.get("BX") + "</td>");
                    result.append("<td>" + _user.get("GUAKE") + "</td>");
                    result.append("<td>" + _user.get("HEIGHT") + "</td>");
                    result.append("<td>" + _user.get("JOB") + "<input type='hidden' name='user[" + num + "].id' value='" +  _user.get("ID") + "' /></td>");
                    result.append("<td>");
                    if (_user.get("STATE").toString().equals("2")) {
                        result.append("已通过");
                    } else if (_user.get("STATE").toString().equals("3")) {
                        result.append("未通过");
                    } else {
                        result.append("审核中");
                    }

                    result.append("</td>");
                    if (_user.get("OP1").equals("未公布")) {
                        result.append("<td class='true'><span>" + _user.get("OP1") + "</span></td>");
                    } else {
                        result.append("<td class='false'><span>" + _user.get("OP1") + "</span></td>");
                    }
                    if (_user.get("FQSTATE").equals("已放弃")
                            && _user.get("FQFID").equals(flowid)) {
                        result.append("<td class='yfq'><span>已放弃</span></td></tr>");
                    } else {
                        if ((int)_user.get("STATE") == EnumTools.zzuserState.未通过.ordinal()) {
                            result.append("<td class='false'><span>已完成</span></td></tr>");
                        } else {
                            boolean bl = _user.get("FID").equals(flowid);
                            result.append("<td class='" + bl + "'><span>" + (bl == true ? "未完成" : "已完成") + "</span></td></tr>");
                        }
                    }
                    num++;
                }
            } else {
                result.append("<tr style='display:none'><td></td></tr>");
            }
            result.append("<input type='hidden' id='pageCount' value='" + pageCount + "' />");
            result.append("<input type='hidden' id='allCount' value='" + allcount + "' />");
            utilService.printTextResult(result.toString(), response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printTextResult("<tr><td colspan='14'>读取数据失败</td></tr><input type='hidden' id='pageCount' value='0' /><input type='hidden' id='allCount' value='0' />", response);
        }

    }
    //[end]

    //[start] 提交审核结果

    @RequestMapping(value = "/RAction", method = RequestMethod.POST)
    public void RAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String fid = request.getParameter("fid").trim();//招聘流程ID
            String deptcode = request.getParameter("deptcode").trim();//责任部门编码
            int state = Integer.parseInt(request.getParameter("state"));
            String[] uids = request.getParameterValues("uid");

            BaseUser cuser = utilService.getSessionUser(request);
            if (cuser.getUdept().equals(deptcode)) {
                //流程是否结束
                if (utilService.FlowIsDoing(fid)) {
                    Date time = new Date();
                    List<HRZZUser> users = recommendService.getuserbyids(uids);
                    List<HRZZUser> editlist = new ArrayList<HRZZUser>();
                    for (HRZZUser user : users) {
                        HRZZUser usr = recommendService.getViewUser(user.getId());
                        if (usr.getDeptcode().equals(cuser.getUdept())
                                //&& usr.getState() != state
                                && usr.getFid().equals(fid)
                                && usr.getFqstate().equals("未放弃")) {
                            usr.setState(state);
                            usr.setCsuid(cuser.getId());
                            usr.setCsuname(cuser.getUname());
                            usr.setCsdate(time);
                            editlist.add(usr);
                        }
                    }
                    if (editlist.size() > 0) {
                        utilService.editList(editlist);
                        utilService.printJsonResult(true, "操作成功！", "no", response);
                    } else {
                        utilService.printJsonResult(false, "无可执行此操作的信息！", "no", response);
                    }
                } else {
                    utilService.printJsonResult(false, "当前流程不支持此操作！", "no", response);
                }
            } else {
                utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }

    //[end]

    //[start] 流程扭转到下一步

    @SuppressWarnings({"rawtypes", "unchecked"})
    @RequestMapping(value = "/NextAction", method = RequestMethod.POST)
    public void NextAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String[] ids = request.getParameterValues("id");//人员信息ID
            if (ids != null) {
                String pid = request.getParameter("pid").trim();//招聘方案ID
                String fid = request.getParameter("fid").trim();//招聘流程ID
                String deptcode = request.getParameter("deptcode").trim();//责任部门编码
                BaseUser usr = utilService.getSessionUser(request);
                if (usr.getUdept().equals(deptcode)) {
                    List editlist = new ArrayList();
                    List<HRZZUser> users = doZZService.getUsers(pid, deptcode, ids);
                    ProjectInfoFlow nFlow = doZZService.getNextFlow(pid, deptcode, fid);
                    for (HRZZUser user : users) {
                        if (user.getFid().equals(fid)
                                && user.getState() == EnumTools.zzuserState.已通过.ordinal()
                                && user.getFqstate().equals("未放弃")) {
                            user.setFid(nFlow.getId());
                            user.setPath(user.getPath() + nFlow.getId() + "/");
                            editlist.add(user);
                        }
                    }

                    if (editlist.size() > 0) {
                        nFlow.setState(EnumTools.PlanFlowState.进行中.ordinal());
                        editlist.add(nFlow);

                        utilService.editList(editlist);
                        utilService.printJsonResult(true, "操作成功！", "no", response);
                    } else {
                        utilService.printJsonResult(false, "当前选中的信息不能执行此操作！", "no", response);
                    }
                } else {
                    utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
                }
            } else {
                utilService.printJsonResult(false, "请选择人员信息！", "no", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }

    //[end]
}
